package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Employee employee,Long[] ids) {
        employeeMapper.insert(employee);
        if(ids!=null&&ids.length>0){
            for (Long rid : ids) {
                employeeMapper.insertRelation(employee.getId(),rid);
            }
        }
    }

    @Override
    public void update(Employee employee,Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //删除关系
        employeeMapper.deleteByEmpId(employee.getId());
        //关联关系
        if(ids!=null&&ids.length>0){
            for (Long rid : ids) {
                employeeMapper.insertRelation(employee.getId(),rid);
            }
        }
    }

    @Override
    public Employee get(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    @Override
    public List<Employee> listAll() {
        List<Employee> list = employeeMapper.selectAll();
        return list;
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        /*if(count==0){
            return new PageResult<>(Collections.emptyList(),qo.getCurrentPage(),qo.getPageSize(),count);
        }else {
            List<Employee> employees = employeeMapper.selectForList(qo);
            return new PageResult<>(employees,qo.getCurrentPage(),qo.getPageSize(),count);
        }*/
         PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageInfo<>(employees);
    }

    @Override
    public Employee login(String username, String password) {
        if(!StringUtils.hasText(username)){
            throw new LogicException("用户名不能为空");
        }
        if(!StringUtils.hasText(password)){
            throw new LogicException("密码不能为空");
        }

        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);
        System.out.println(employee);
        if(employee==null){
            throw new LogicException("用户名或者密码错误");
        }
        return employee;
    }

    @Override
    public Workbook exportXls() {
        List<Employee> employees = employeeMapper.selectAll();
        //创建工作簿excel
        Workbook workbook = new HSSFWorkbook();
        //创建工作表
        Sheet sheet = workbook.createSheet("通讯录");
        //创建标题行
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("邮箱");
        row.createCell(3).setCellValue("年龄");
        for (int i = 0; i < employees.size() ; i++) {
            //创建行
            row = sheet.createRow(i+1);
            Employee employee = employees.get(i);
            //创建单元格
            row.createCell(0).setCellValue(employee.getUsername());
            row.createCell(1).setCellValue(employee.getName());
            row.createCell(2).setCellValue(employee.getEmail());
            if(employee.getAge()!=null){
                row.createCell(3).setCellValue(employee.getAge());
            }
        }
        return workbook;
    }

    @Override
    public Employee selectByUsername(String username) {
        return employeeMapper.selectByUsername(username);
    }

    public void importXls(MultipartFile file) throws IOException {
        //把接收到的文件以excel方式去读取并操作
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        //读取第一页
        Sheet sheet = workbook.getSheetAt(0);
        //获取最后一行的索引
        int lastRowNum = sheet.getLastRowNum();
        //从索引为1的行开始读(忽略表头)
        for (int i = 0; i <=lastRowNum ; i++) {
            //读取行数据
            Row row = sheet.getRow(i + 1);
            String username = row.getCell(0).getStringCellValue();
            //判断如果用户名是空就不再往下读了
            if(!StringUtils.hasLength(username.trim())){
                return;
            }
            Employee employee = new Employee();
            employee.setUsername(username);
            employee.setName(row.getCell(1).getStringCellValue());
            employee.setEmail(row.getCell(2).getStringCellValue());
            employee.setAge((int)row.getCell(3).getNumericCellValue());
            employee.setPassword("88888888");
            /*if(row.getCell(i).getCellType()== CellType.NUMERIC){
                //
            }*/
            employeeMapper.insert(employee);
        }
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }
}
